import time

from meiduo_mall.libs.CCP.SDK.CCPRestSDK import REST

from meiduo_mall.libs.CCP.constants import SMS_SEND_INTERVAL, SMS_SEND_TIMES

# 配置发送短信的参数
accountSid = "8a216da86b2bc78f016b46aef276125a"
accountToken = '69c946ae2e2040389e785b83394b281e'
appId = '8a216da86b2bc78f016b46aef2cb1260'
serverIP = "sandboxapp.cloopen.com"
serverPort = "8883"
softVersion = "2013-12-26"

# 不清楚发送失败是返回对应的状态码还是直接报错
def send_SMS(to, datas, tempId):
    for _ in range(SMS_SEND_TIMES):
        rest = REST(serverIP, serverPort, softVersion)
        rest.setAccount(accountSid, accountToken)
        rest.setAppId(appId)
        try:
            result = rest.sendTemplateSMS(to, datas, tempId)
        except Exception as e:
            return -1
        else:
            if result["statusCode"] == "000000":
                # 返回0表示发送成功
                return 0
            time.sleep(SMS_SEND_INTERVAL)
    else:
        # 返回1表示发送失败
        return -1
